"use strict";

var geeurlcodeObj = geeurlcodeObj(),
    favorite,
    page = 1,
    pageSize = 10;
toastr.options = {
  closeButton: false,
  debug: false,
  progressBar: true,
  positionClass: "toast-top-center",
  onclick: null,
  showDuration: "300",
  hideDuration: "1000",
  timeOut: "2000",
  extendedTimeOut: "1000",
  showEasing: "swing",
  hideEasing: "linear",
  showMethod: "fadeIn",
  hideMethod: "fadeOut"
};

function titleTop() {
  $('#word').text("" + geeurlcodeObj.word);
  $('#childTitle').html("" + geeurlcodeObj.childTitle);
}

function loading() {
  $.server({ contentId: geeurlcodeObj.id }, '/website/v1/contentDetail').then(function (res) {
    var obj = res.result;
    $('#title').html("" + obj.title);
    $('#author').html("" + obj.author);
    $('#createTime').html("" + obj.createTime);
    $('#hit').html("" + obj.hit);
    $('#commentCount').html("" + obj.commentCount);
    $('#detail').html("" + obj.detail);
    // $('#poster').attr("src",obj.poster);
    favorite = obj.favorite;
    if (obj.favorite == 1) {
      $('.favorite_active').show();
      $(".favorite_defult").hide();
    } else {
      $('.favorite_active').hide();
      $(".favorite_defult").show();
    }
  });
}

//收藏 取消收藏
function colletThis() {
  if (validLogin()) {
    var params = {
      bizType: 1,
      bizId: geeurlcodeObj.id
    };
    if (favorite == 0) {
      //收藏
      $.server(params, '/iagent/v1/addFavorite').then(function (res) {
        toastr.info('收藏成功');
        loading(geeurlcodeObj.id);
      });
    } else {
      //取消收藏
      $.server(params, '/iagent/v1/delFavorite').then(function (res) {
        toastr.info('取消收藏成功');
        loading(geeurlcodeObj.id);
      });
    }
  }
}

//评价
function commentClick() {
  var avlue_textarea = $("#avlue_textarea").val();
  if (!avlue_textarea) {
    return toastr.info('说点什么吧');
  }
  var params = {
    "bizType": 1, "bizId": geeurlcodeObj.id, "content": avlue_textarea
  };
  $.server(params, '/iagent/v1/addComment').then(function (res) {
    toastr.info('评价成功');
    $("#avlue_textarea").val("");
    page = 1;
    showAvlue();
  });
}

//展示评论
function showAvlue() {
  $.server({ bizId: geeurlcodeObj.id, bizType: 1, pageNum: page, pageSize: pageSize }, '/iagent/v1/queryBizCommentList').then(function (res) {
    $(".avlue_toggle").fadeOut();
    var obj = res.result.data;
    if (page == 1) {
      $(".avlue_list").html("");
    }
    var html = '';
    for (var i = 0; i < obj.length; i++) {
      html += "<div class=\"flexLay commentBox\">\n            <img src=\"" + (obj[i].avatar || "images/defultHeader.png") + "\" class=\"avatar\" alt=\"\">\n            <div class=\"content\">\n                <div class=\"userName\">" + obj[i].userName + "</div>\n                <div class=\"timer\">" + obj[i].createTime + "</div>\n                <div class=\"descipt\">" + obj[i].comment + "</div>\n            </div>\n        </div>";
    }
    $(".avlue_list").append(html);
    $(".avlueBox").fadeIn();
    if (res.result.totalPage <= page) {
      $(".viewMore").fadeOut();
      $(".nocontent").fadeIn();
    } else {
      $(".viewMore").fadeIn();
      $(".nocontent").fadeOut();
    }
  });
}

//查看更多
function viewMore() {
  page++;
  this.showAvlue();
}
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  //调用server加载首页新闻
  loading();
  titleTop();
});